package com.huaxin.common.tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


/**
 * Created by Administrator on 2017/1/19.
 */

public class CompressUtil {
    private static int MIN_WIDTH = 0;
    private static int MIN_HEIGHT = 0;
    private static int MAX_WIDTH = 320;
    private static int MAX_HEIGHT = 320;


    public static Bitmap compress(String path) {
       return compress(path,MIN_WIDTH,MIN_HEIGHT,MAX_WIDTH,MAX_HEIGHT);
    }

    public static Bitmap compress(String _path, int mMinWidth, int mMinHeight, int mMaxWidth,
                                  int mMaxHeight) {
        BitmapFactory.Options option = new BitmapFactory.Options();
        Bitmap bitmap = null;
        if (mMaxWidth <= 0 && mMaxHeight <= 0) {
            bitmap = BitmapFactory.decodeFile(_path);
        } else {
            option.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(_path,option);
            int outWidth = option.outWidth;
            int outHeight = option.outHeight;
            Log.d("Zoes",outWidth+","+outHeight);

            if (outWidth < mMaxWidth && outHeight < mMaxHeight) {
                option.inJustDecodeBounds = false;
                bitmap =BitmapFactory.decodeFile(_path,option);
            } else {

                // 计算出图片应该显示的宽高
                int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight,
                        outWidth, outHeight);
                int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth,
                        outHeight, outWidth);

                //如果计算后的宽度小于要求的最小宽度
                //TLog.error("处理前的size：" + desiredWidth + " x " + desiredHeight);

                //处理长图情况，例如一张。400*8000的图，或8000*400的图。
                //这里 要么 宽小于预设的最小宽度，要么高小于预设的最小高度。不会出现 管和高都小于最小宽高度的情况。
                //宽度小于最小值
                if (desiredWidth < mMinWidth) {
                    float scale = (float) desiredWidth / mMinWidth;
                    desiredWidth = mMinWidth;
                    desiredHeight = (int) (desiredHeight / scale);
                } else
                    //高度小于最小值
                    if (desiredHeight < mMinHeight) {
                        float scale = (float) desiredHeight / mMinHeight;
                        desiredHeight = mMinHeight;
                        desiredWidth = (int) (desiredWidth / scale);
                    }

                //TLog.error("处理后的size：" + desiredWidth + " x " + desiredHeight);
                option.inJustDecodeBounds = false;
                option.inSampleSize = findBestSampleSize(outWidth, outHeight, desiredWidth, desiredHeight);

                Bitmap tempBitmap = BitmapFactory.decodeFile(_path,option);;

                // 做缩放
                if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
                    bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
                    tempBitmap.recycle();
                    //缩放完后如果还是大，剪裁.
                    //分两种情况，一种是高超标，一种是宽超标
                    if (bitmap.getWidth() > mMaxWidth || bitmap.getHeight() > mMaxHeight) {
                        tempBitmap = bitmap;
                        int width = bitmap.getWidth() > mMaxWidth ? mMaxWidth : bitmap.getWidth();
                        int height = bitmap.getHeight() > mMaxHeight ? mMaxHeight : bitmap.getHeight();
                        bitmap = Bitmap.createBitmap(tempBitmap, 0, 0, width, height);
                        tempBitmap.recycle();
                    }
                } else {
                    bitmap = tempBitmap;
                }
            }
        }
        return bitmap;
    }
    private static int findBestSampleSize(int actualWidth, int actualHeight,
                                          int desiredWidth, int desiredHeight) {
        double wr = (double) actualWidth / desiredWidth;
        double hr = (double) actualHeight / desiredHeight;
        double ratio = Math.min(wr, hr);
        float n = 1.0f;
        while ((n * 2) <= ratio) {
            n *= 2;
        }
        return (int) n;
    }
    private static int getResizedDimension(int maxPrimary, int maxSecondary,
                                           int actualPrimary, int actualSecondary) {
        if (maxPrimary == 0 && maxSecondary == 0) {
            return actualPrimary;
        }
        if (maxPrimary == 0) {
            double ratio = (double) maxSecondary / (double) actualSecondary;
            return (int) (actualPrimary * ratio);
        }

        if (maxSecondary == 0) {
            return maxPrimary;
        }

        double ratio = (double) actualSecondary / (double) actualPrimary;
        int resized = maxPrimary;
        if (resized * ratio > maxSecondary) {
            resized = (int) (maxSecondary / ratio);
        }
        return resized;
    }


}
